<?php
/**
 * Created by PhpStorm.
 * User: lcz
 * Date: 2018/8/16
 * Time: 17:44
 * 中奖后生成的订单
 */

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

class Order extends Api {
	
	protected $noNeedRight = ['*'];
	
	
	public function index(){
		$userId = $this->auth->id;
		$page = input('post.page', 1, 'int');
		
		$rows = Db::name('order')->alias('o')
			->join('goodsRecord r', 'o.goods_record_id = r.id', 'left')
			->join('goods g', 'r.goods_id = g.id', 'left')
			->where(['o.user_id' => $userId])
			->order(['o.create_time' => 'DESC'])
			->field('o.id, o.goods_record_id, g.goods_name, g.image, from_unixtime(o.create_time) as time, o.order_status, o.shipping_status, o.comment_status, o.shipping_name, o.shipping_no')
			->page($page)
			->select();
		
		$this->success(__('查询成功'), $rows);
	
	}
	
	public function detail(){
	
	}
	
	public function sign() {
		$userId = $this->auth->id;
		$id = input('post.id', '', 'int');
		if(!$id || $id < 0) {
			$this->error(__('参数错误'));
		}
		
		$row = Db::name('order')
			->where(['user_id' => $userId, 'id' => $id])
			->find();
		
		if(!$row) {
			$this->error(__('数据不存在'));
		}
		
		if($row['order_status'] != \app\common\model\Order::STATUS_S){
			$this->error(__('非待收货订单'));
		}
		
		$res = Db::name('order')
			->where(['id' => $row['id']])
			->update([
				'order_status' => \app\common\model\Order::STATUS_F,
				'shipping_status' => 2,
				'sign_time' => time(),
			]);
		if($res) {
			$this->success(__('签收成功'));
		} else {
			$this->error(__('签收出错'));
		}
	}
	
	public function commont(){
	
	}
}
